package customview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.example.sinahama.myui.R;

import static android.R.color.transparent;

/**
 * Choose CheckBox of LinerLayout
 */

public class ChooseQuestionLinerLayout extends LinearLayout {


    //创建 checkbox 的数量
    private int checkboxSize = 4;


    public ChooseQuestionLinerLayout(Context context) {
        //super(context);
        this(context, null);
    }

    public ChooseQuestionLinerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public ChooseQuestionLinerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        greateView();
    }

    private Context context;

    /**
     * 初始化LinearLayout视图
     */
    private void greateView() {
        // 设置LinearLayout的布局方向
        setOrientation(LinearLayout.HORIZONTAL);
        // 设置布局参数
        LinearLayout.LayoutParams params = new LayoutParams(
                0, LayoutParams.WRAP_CONTENT, 1);


        //添加到布局中
        for (int i = 0; i < checkboxSize; i++) {
            final CheckBox checkBox = new CheckBox(getContext());
            InnerCheckBoxTag innerCheckBoxTag = new InnerCheckBoxTag();
            innerCheckBoxTag.idextag = i;
            checkBox.setTag(innerCheckBoxTag);// 设置标签
            if (backgroundSelector != 0) {
//                checkBox.setButtonDrawable(17170445);
                checkBox.setButtonDrawable(android.R.color.transparent);
                checkBox.setBackgroundResource(backgroundSelector);
            }

            checkBox.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    disoseCheckBox(checkBox);

                }
            });
            addView(checkBox, params);

        }


    }

    // 获取创建的数量
    public int getCheckboxSize() {
        return checkboxSize;
    }

    //设置创建的数量
    public void setCheckboxSize(int checkboxSize) {
        this.checkboxSize = checkboxSize;
        removeAllViews();
        greateView();
    }


    // checkBox 被点击后的状态处理
    private void disoseCheckBox(CheckBox checkBox) {
        // onClick
        InnerCheckBoxTag currenCheckBox = (InnerCheckBoxTag) checkBox.getTag();
        // 判断checkBox 是否是被选中
        boolean ischeck = checkBox.isChecked();
        // 如果选中 点击后去掉
        Log.d("MMM", "  " + ischeck);
        if (ischeck) {

            // 如果没有选中 将其他的选中去掉
            for (int j = 0; j < checkboxSize; j++) {
                if (j == currenCheckBox.idextag) {

                } else {
                    ((CheckBox) getChildAt(j)).setChecked(false);

                }
            }
        }
    }

    public int getBackgroundSelector() {
        return backgroundSelector;
    }

    //设置背景选择器
    public void setBackgroundSelector(int backgroundSelector) {
        this.backgroundSelector = backgroundSelector;
        removeAllViews();
        greateView();
    }

    public void setContext(Context context) {
        this.context = context;
    }

    //设置selector
    private int backgroundSelector; // 资源id

    /**
     * CheckBox Tag
     */
    class InnerCheckBoxTag {
        int idextag;//  index of layout

    }
}


